#include "listwidget.h"

ListWidget::ListWidget(QWidget *parent)
    : QWidget{parent}
{
    setStyleSheet("QWidget {"
                  "    background-color: #f0f3f6;"
                  "    border-radius: 10px;"
                  "}");
    setGeometry(0,75,200,parent->height()-150);
    setWindowFlags(Qt::FramelessWindowHint);

    scrollarea=new QScrollArea(parent);
    scrollarea->setGeometry(20,75,180,parent->height()-75);
    scrollarea->setWidget(this);
    scrollarea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    scrollarea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    scrollarea->setWidgetResizable(true);
    scrollarea->setStyleSheet("   border-radius: 10px; background-color: #f0f3f6;"
                              "   QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {"
                              "     background: none;"  // 隐藏上下箭头
                              "}");
    QScrollBar*bar=scrollarea->verticalScrollBar();
    bar->setFixedWidth(7);
    bar->setStyleSheet("   background-color: #f0f3f6;"  // 背景颜色
                       "   border-radius: 10px;"         // 圆角
                       "   border: none;" );

    vlayout=new QVBoxLayout(this);
    buttons.resize(4,QVector<PushButton*>());

    QStringList strlist;
    strlist<<"推荐"<<"精选"<<"播客"<<"漫游"<<"动态"<<"我的"<<"我喜欢的音乐"<<"最近播放"<<"下载管理"<<"我的播客";
    strlist<<"我的收藏"<<"本地音乐"<<"创建歌单"<<"收藏的歌单";
    int iconcount=0;
    for(int i=0;i<strlist.size();i++)
    {
        auto button=new PushButton;
        button->setText(strlist[i]);
        if(strlist[i]=="本地音乐")
            connect(button,&QPushButton::clicked,this,[this]{emit localMusicWidget();});
        else if(strlist[i]=="我喜欢的音乐")
            connect(button,&QPushButton::clicked,this,[this]{emit myLikeWidget();});
        else if(strlist[i]=="推荐")
            connect(button,&QPushButton::clicked,this,[this]{emit recommendWidget();});
        else if(strlist[i]=="最近播放")
            connect(button,&QPushButton::clicked,this,[this]{emit historyMusicWidget();});
        if(i!=5&&i!=12&&i!=13)
        {
            QIcon icon(QString(":/icon/%1.png").arg(QString::number(iconcount++)));
            button->setIcon(icon);
        }
        if(i!=5&&i!=12&&i!=13)
        {
            SetButtonStyle(button);
            connect(button,&QPushButton::clicked,this,[=](){onClickButton(button);});
        }
        else//这三个是不需要维持变色的
        {
            button->setMinimumSize(QSize(75,25));
            button->setFont(QFont("Microsoft YaHei",8,40));
            button->setStyleSheet("QPushButton{text-align: left;color:#222222}"
                                  "QPushButton:hover{color:#888888}");
            //用于折叠或展开的信号与槽
            connect(button,&QPushButton::clicked,this,[=]{onClickFoldButton(button);});
        }
        if(i<5)
            buttons[0].push_back(button);
        else if(i<=11&&i>=5)
            buttons[1].push_back(button);
        else if(i==12)
            buttons[2].push_back(button);
        else
            buttons[3].push_back(button);

        button->hide();

        //只显示部分功能
        if(i==0)
            button->show();
        else if(i==11||i==5||i==6||i==7)
            button->show();
        else if(i==12)
            button->show();
    }
    lastClickedButton=buttons[0][0];
    onClickButton(buttons[1][1]);

    for(int i=0;i<3;i++)
    {
        QFrame *line = new QFrame(this);
        line->setFixedHeight(1);  // 设置高度为2像素
        line->setStyleSheet("background-color: #dcdcdc;");  // 设置颜色
        frames.push_back(line);
    }

    int buttoncount1=0,buttoncount2=0,framecount=0;
    qint32 sum=0;
    for(int i=0;i<buttons.size();i++)
    {
        sum+=buttons[i].size();
    }
    for(int i=0;i<frames.size()+sum;i++)
    {
        if(i==5||i==13||i==15)
        {
            vlayout->addWidget(frames[framecount++]);
        }
        else
        {
            vlayout->addWidget(buttons[buttoncount1][buttoncount2++]);
            if(buttoncount2==buttons[buttoncount1].size())
            {
                buttoncount1++;
                buttoncount2=0;
            }
        }
    }

    vlayout->addStretch(1);
}

void ListWidget::SetButtonStyle(ListWidget::PushButton*button)
{
    button->setMinimumSize(QSize(75,40));
    button->setFont(QFont("Microsoft YaHei",11,100));
    button->setStyleSheet( "QPushButton {"
                          "   text-align: left;"
                          "   background-color: #f0f3f6;"  // 背景颜色
                          "   border-radius: 10px;"         // 圆角
                          "   padding: 10px;"
                          "   border: none;"   // 边框颜色
                          "   color:#787f8d;"
                          "}"
                          "QPushButton:hover {"
                          "   background-color: #e4e8ec;"  // 鼠标悬停时的背景颜色
                          "   border-radius: 10px;"         // 保持圆角
                          "}"
                          "QPushButton:pressed {"
                          "   background-color: #ea3e3e;"  // 按钮被按下时的背景颜色
                          "   border-radius: 10px;"         // 保持圆角
                          "}"
                          "QPushButton:focus {"
                          "   outline: none;"               // 移除焦点框
                          "}");
    return;
}

void ListWidget::onMainWindowResize(QSize size)
{
    scrollarea->resize(scrollarea->width(),size.height()-75);
    this->resize(scrollarea->width(),size.height()-75);
    scrollarea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
}

void ListWidget::AddCreatePlayList(const QVector<PushButton*>&list)
{
    for(int i=0;i<list.size();i++)
    {
        list[i]->setParent(this);
        buttons[2].push_back(list[i]);
        auto btn=list[i];
       connect(list[i],&QPushButton::clicked,this,[=](){onClickButton(btn);});
    }

}
void ListWidget::AddFavoritePlayList(const QVector<PushButton*>&list)
{
    for(int i=0;i<list.size();i++)
    {
        list[i]->setParent(this);
        buttons[3].push_back(list[i]);
    }
}

void ListWidget::AddCreatePlayList1(const QStringList&strlist)
{
    strlists<<strlist;
    QVector<PushButton*>vec;
    for(auto&elem:strlist)
    {
        auto ptr=new PushButton;
        SetButtonStyle(ptr);
        ptr->setText(elem);
        vec.push_back(ptr);
        vlayout->insertWidget(vlayout->count()-1,ptr);
        connect(ptr,&PushButton::clicked,this,[ptr,this](){
            for(int i=0;i<buttons[2].size();i++)
            {
                if(ptr==buttons[2][i])
                    emit clickedButton(i-1);
            }
        });
    }
    AddCreatePlayList(vec);

}

void ListWidget::ClearClickedStyle()
{
    SetButtonStyle(lastClickedButton);
}

void ListWidget::onClickButton(PushButton* button)
{
    SetButtonStyle(lastClickedButton);
    lastClickedButton=button;
    button->setStyleSheet("QPushButton {"
                          "   text-align: left;"
                          "   padding: 5px;"
                          "   background-color: #ea3e3e;"  // 背景颜色
                          "   border-radius: 10px;"         // 圆角
                          "   border: none;"   // 边框颜色
                          "}");
}

void ListWidget::onClickFoldButton(PushButton*button)
{
    if(button->text()=="我的")
    {
        //RemoveOrInsertButton(1);
    }
    else if(button->text()=="创建歌单")
    {
        //RemoveOrInsertButton(2);
        emit createPlayList();
    }
    else if(button->text()=="收藏的歌单")
    {
        //RemoveOrInsertButton(3);
    }
}

void ListWidget::RemoveOrInsertButton(qint64 order)
{
    qint32 count=0;
    if(buttons[order].size()!=1)
    {
        if(buttons[order].last()->remove==false)//移除时
        {
            for(int i=buttons[order].size()-1;i!=0;i--)
            {
                vlayout->removeWidget(buttons[order][i]);
                buttons[order][i]->remove=true;
                buttons[order][i]->hide();
            }
        }
        else//添加时
        {
            for(int i=0;i<order;i++)
                count=buttons[i].size()+1;
            count++;
            for(int i=buttons[order].size()-1;i!=0;i--)
            {
                vlayout->insertWidget(count,buttons[order][i]);
                buttons[order][i]->remove=false;
                buttons[order][i]->show();
            }
        }
    }
}


